<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>call方法</title>
</head>
<body>

</body>
</html>
<script>
    // function fn() {
    //     console.log(this);
    // }
    // fn();
    // var obj = {name:'ha'};
    // fn.call(obj);



    // function sum() {
    //     console.log(this);
    // }
    // function fn() {
    //     console.log(this);
    // }
    // var obj = {name:'iceman'};
    // Function.prototype.myCall = function (context) {
    //     console.log(this.toString().replace('this','obj'));
    //     //原理：伪代码
    //     var a = eval(this.toString().replace('this','obj'));
    //     this();
    // };
    // fn.myCall(obj);
    // sum.myCall(obj);



    function fn1() {
        console.log(1);
        console.log(this);
    }
    function fn2() {
        console.log(2);
        console.log(this);
    }
    fn1.call(fn2);
    fn1.call.call(fn2);

    // Function.prototype.call = test1;
    // function test1(content) {
    //     this();
    // }
    //
    // Function.prototype.call = function (context) {
    //     this();
    // }
    //
    //

    var nodes = [
			{"id":2,"cname":"第一级1","ipid":0,"ccode":"1"},
			{"id":3,"cname":"第二级1","ipid":2,"ccode":"1"},
			{"id":4,"cname":"第二级1","ipid":2,"ccode":"1"},
			{"id":5,"cname":"第三级1","ipid":4,"ccode":"1"},
			{"id":6,"cname":"第三级1","ipid":3,"ccode":"1"},
			{"id":7,"cname":"第一级1","ipid":0,"ccode":"1"}
		];
    function transDate(list,idstr,pidstr){
        var result = [],temp = {};
        for(i = 0; i < list.length; i++){
            temp[list[i][idstr]]=list[i];//将nodes数组转成对象类型
        }
        for(j=0; j<list.length; j++){
            tempVp = temp[list[j][pidstr]]; //获取每一个子对象的父对象
            if(tempVp){//判断父对象是否存在，如果不存在直接将对象放到第一层
                if(!tempVp["nodes"]) tempVp["nodes"] = [];//如果父元素的nodes对象不存在，则创建数组
                tempVp["nodes"].push(list[j]);//将本对象压入父对象的nodes数组
            }else{
                result.push(list[j]);//将不存在父对象的对象直接放入一级目录
            }
        }
        return result;
    }

    console.log(transDate(nodes,"id","ipid")); //传入的值 列表 ，本对象的id，父对象的id
    document.write(JSON.stringify(transDate(nodes,"id","ipid")));
</script>